package Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import model.PersonModel;

/** 
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		String path="";
		String content1 = "";
		String content2 = "";
		boolean flag = false;
		UserDao userDao = new UserDao();
		
		
		
		request.setCharacterEncoding("UTF-8");
	
		
		String username = request.getParameter("username");
				
		String password = request.getParameter("password");
		
		String authcode = request.getSession().getAttribute("authCode").toString();
		
		String code = request.getParameter("code");
		
		
		String sex = request.getParameter("sex");
		String hobbys[] = request.getParameterValues("hobby");
		String address = request.getParameter("address");
		String hobby="";
		for(String s:hobbys) {
			hobby+=s+" ";
		}
		
	 
		
		
		PersonModel person = new PersonModel();
		
		person.setUsername(username);
	
		
		person = userDao.selectSingle(person);
		
	
	 if(code.equals(authcode)) { 
		
			
		if(person.getUsername()!=null) { 
			flag=true;
			content1="对不起，该账户已经存在了";
			content2="重新注册";		
			path="/Test2022/web/js/register.jsp";
			
			  
			
		}else {
			content1="true";
			person.setUsername(username);
			person.setAddress(address);
			person.setSex(sex);
			person.setHobby(hobby);
			person.setPassword(password);
			
			userDao.insertSingle(person);
			System.out.println("注册成功,跳转登录界面");
			request.getSession().setAttribute("content1", content1);
			
			response.sendRedirect("/Test2022/web/js/login.jsp");
		}
		
		
		
		
		}else {
			flag=true;
			content1="验证码错误，请重新输入";
			content2="重新输入";		
			path="/Test2022/web/js/register.jsp";
			
			
		}
		
		if (flag) {
			request.getSession().setAttribute("content1", content1);
			request.getSession().setAttribute("content2", content2);
			request.getSession().setAttribute("path", path);
			
			response.sendRedirect("/Test2022/web/js/PasswordWrong.jsp");
			
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		
		
	}

}
